<template>
  <!-- 商品列表组件 -->
  <div class="goods-container">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片试图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="10">
        <el-col :span="10">
          <!-- @clear：当点击清空按钮时触发的事件 -->
          <el-input
            clearable
            placeholder="请输入内容"
            class="searchInp"
            v-model="queryInfo.query"
            @clear="getGoodsList()"
            @keyup.enter.native="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              class="searchBtn"
              @click="getGoodsList()"
            ></el-button>
          </el-input>
        </el-col>

        <el-col :span="2">
          <el-button type="primary" @click="goAddGoodsPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table border stripe size="mini" :data="goodsList" height="calc(78%)">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称"
          width="300"
        ></el-table-column>
        <el-table-column prop="goods_price" label="价格"></el-table-column>
        <el-table-column prop="goods_number" label="数量"></el-table-column>
        <el-table-column prop="goods_number" label="数量"></el-table-column>
        <el-table-column prop="goods_weight" label="重量"></el-table-column>
        <el-table-column prop="goods_state" label="商品状态"></el-table-column>
        <el-table-column prop="add_time" label="添加时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat('yyyy-MM-dd hh:mm:ss') }}
          </template>
        </el-table-column>
        <el-table-column prop="upd_time" label="更新时间" width="140">
          <template slot-scope="scope">
            {{ scope.row.upd_time | dateFormat('yyyy-MM-dd hh:mm:ss') }}
          </template></el-table-column
        >
        <el-table-column prop="hot_mumber" label="热销数量"></el-table-column>
        <!-- <el-table-column
          prop="is_promote"
          label="是否是热销品"
        ></el-table-column> -->
        <el-table-column
          fixed="right"
          label="操作"
          width="120"
          header-align="center"
          align="right"
        >
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="goEditGoodsPage(scope.row.goods_id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="removeGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页条区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取商品列表请求参数的对象
      queryInfo: {
        query: '', // 搜索关键字
        pagenum: 1, // 当前页码
        pagesize: 10 // 每页显示条数
      },
      // 商品列表
      goodsList: [],
      // 总数据条数
      total: 0
    }
  },
  created() {
    // 获取商品列表数据
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表数据
    async getGoodsList() {
      // 发送请求
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      // 判断数据获取是否成功
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听 pageSize（页面显示条数） 改变的事件
    handleSizeChange(newSize) {
      // newSize：新的页面显示条数
      this.queryInfo.pagesize = newSize
      // 重新获取商品列表数据
      this.getGoodsList()
    },
    // 监听 pagenum（页码值） 改变的事件
    handleCurrentChange(newPage) {
      // newPage：新的页码值
      this.queryInfo.pagenum = newPage
      // 重新获取商品列表数据
      this.getGoodsList()
    },
    // 根据 id 删除商品
    async removeGoods(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果用户确认删除，则返回字符串 confirm
      // 如果用户取消删除，则返回字符串 cancel
      if (confirmResult === 'cancel') return

      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除商品失败！')
      this.$message.success('删除成功！')
      this.getGoodsList()
    },
    // 跳转到添加页面
    goAddGoodsPage() {
      this.$router.push('/goods/add')
    },
    //
    goEditGoodsPage(goodsId) {
      this.$router.push('/goods/edit/' + goodsId)
    }
  },
  computed: {},
  watch: {},
  filters: {}
}
</script>

<style lang="less" scoped>
.goods-container {
  height: 100%;
  max-height: 100%;
  overflow: auto;

  // 搜索框
  .searchInp {
    outline: none !important;
    border: none !important;
  }

  // 搜索按钮
  .searchBtn {
    background-color: #369d69;
    outline: none;
    border-radius: 3px;
    color: #fff;
    opacity: 0.8;

    &:hover {
      opacity: 0.6;
    }

    &:active {
      opacity: 1;
    }
  }
}
</style>
